const query = require('../../db/db')
const url = require('url')
const upload = require('../../multer/upload');
//上传图片 并修改数据库地址  包括上传文章图片 本应该写两个接口 在此写一个 
exports.uploadPicture = async (req, res) => {
    try {
        let arg = url.parse(req.url)
        let email = req.auth.email
        if (arg.query) {
            const arr = arg.query.split('=')
            let obj = getUrl(arr)
            console.log("objs", obj.type);
            upload(req, res).then(imgsrc => {
                return res.send({
                    "errno": 0, // 注意：值是数字，不能是字符串
                    "data": {
                        "url": imgsrc, // 图片 src ，必须
                    }
                })
            }, err => {
                return res.send({
                    "errno": 1, // 只要不等于 0 就行
                    "message": "上传失败" + err,
                })
            })
        } else {
            console.log(456);
            upload(req, res).then(imgsrc => {
                let result = updfiles(imgsrc, email)
                if (result) {
                    return res.send({
                        "errno": 0, // 注意：值是数字，不能是字符串
                        "data": {
                            "url": imgsrc, // 图片 src ，必须
                        }
                    })
                } else {
                    return res.send({
                        "errno": 1, // 只要不等于 0 就行
                        "message": "上传失败,数据库异常",
                    })
                }
            }, err => {
                return res.send({
                    "errno": 1, // 只要不等于 0 就行
                    "message": "上传失败" + err,
                })
            })
        }
    } catch (error) {
        console.log(error);
        res.cc('服务器异常')
    }
}
//上传封面图片
exports.uploadCover = async (req, res) => {
    try {
        upload(req, res).then(imgsrc => {
            return res.send({
                "errno": 0, // 注意：值是数字，不能是字符串
                "data": {
                    "url": imgsrc, // 图片 src ，必须
                }
            })
        }, err => {
            return res.send({
                "errno": 1, // 只要不等于 0 就行
                "message": "上传失败" + err,
            })
        })

    } catch (error) {
        console.log(error);
        res.cc('服务器异常')
    }
}
async function updfiles(imgsrc, email) {
    let sql = 'UPDATE ev_users_email set files=? where email=?'
    const result = await query(sql, [imgsrc, email])

    const searchDis = 'select * from discuss where email=?'
    const resu = await query(searchDis, email)
    if (resu.length) {
        const updReply = `update discuss set files=? where email=?`
        const result = await query(updReply, [imgsrc, email])
        console.log('修改头像', result);
    }

    const searchre = 'select * from reply_discuss where replyEmail=?'
    const resssre = await query(searchre, email)
    if (resssre.length) {
        const updReply = `update reply_discuss set files=? where email=?`
        const result = await query(updReply, [imgsrc, email])
        console.log('修改头像', result);
    }


    const searchs = 'select * from reply_discuss where replyEmail=?'
    const resss = await query(searchs, email)
    if (resss.length) {
        const updReply = `update reply_discuss set replyFiles=? where replyEmail=?`
        const result = await query(updReply, [imgsrc, email])
        console.log('修改头像', result);
    }



    if (result.affectedRows > 1) {
        return true
    }
    return false
}
//获取url 参数 将数组对象化  简单数据
function getUrl(arr) {
    try {
        let obj = {}
        obj[arr[0]] = arr[1]
        return obj
    } catch (error) {

    }
}